$(document).ready(function(){
	activeMenu('2', '4');
	isReadyToSearch('smartSearchBtn');
	isReadyToSearch('demographicsSearchBtn');
	isReadyToSearch('dailySearchBtn');
	$('#demographicsSearchBtn').click(function() {
		demogSearch();
	});
	
	$('#searchstring').keypress(function(e) {
		if (e.which == 13){
			smartSearch();
		}
	});
	
	$('#code').keypress(function(e) {
		if (e.which == 13){
			demogSearch();
		}
	});	
	
	$('#name').keypress(function(e) {
		if (e.which == 13){
			demogSearch();
		}
	});		
	
	$('#date').keypress(function(e) {
		if (e.which == 13){
			demogSearch();
		}
	});		
	
	$('#phone').keypress(function(e) {
		if (e.which == 13){
			demogSearch();
		}
	});		

	$('#career').keypress(function(e) {
		if (e.which == 13){
			demogSearch();
		}
	});	

	$('#email').keypress(function(e) {
		if (e.which == 13){
			demogSearch();
		}
	});	

	$('#street').keypress(function(e) {
		if (e.which == 13){
			demogSearch();
		}
	});	

	$('#ward').keypress(function(e) {
		if (e.which == 13){
			demogSearch();
		}
	});	
		
	$('#district').keypress(function(e) {
		if (e.which == 13){
			demogSearch();
		}
	});	
	
	$('#province').keypress(function(e) {
		if (e.which == 13){
			demogSearch();
		}
	});		
	
	$('#daily_name').keypress(function(e) {
		if (e.which == 13){
			dailySearch();
		}
	});	

	$('#daily_code').keypress(function(e) {
		if (e.which == 13){
			dailySearch();
		}
	});	

	$('#dailySearchBtn').click(function(){
		dailySearch();
	});

	$('#smartSearchBtn').click(function(){
		smartSearch();
	});
	
	$('.nav_bg a').click(function() {
		$('.nav_bg a').removeClass('active');
		$(this).addClass('active');
	});
		
});

function dailySearch(){
	var daily_name = $('#daily_name').val();
	$('#daily_name').val($.trim(daily_name));
	var daily_code = $('#daily_code').val();
	$('#daily_code').val($.trim(daily_code));
	
    $('#dailySearchBtnDiv').css('display', 'none');
	$('#dailyLoading').css('display', 'block');	
	$.get(
		'../PatientFinder/searchAjax?set_ajax_view',
		{
			daily_code: $('#daily_code').val(),
			daily_name: $('#daily_name').val(),
			search_type: 'daily_search'
		},
		function (data){
			$('div#dailyResult').html(data.header + data.body);
			$('#dailySearchBtnDiv').css('display', 'inline');
			$('#dailyLoading').css('display', 'none');
			
			// Update default number of rows per page
			$('#dailyPaging select').val('10');
			
			serviceTable = $('div#dailyResult table');
			if ($('tbody tr', serviceTable).length > 1) {
				$("#dailyPaging").show();
				serviceTable.trigger("update");	
				    // add parser through the tablesorter addParser method 
			    $.tablesorter.addParser({ 
			        // set a unique id 
			        id: 'created', 
			        is: function(s) { 
			            // return false so this parser is not auto detected 
			            return false; 
			        }, 
			        format: function(s) { 
			            // format your data for normalization 
			            return s;
			        }, 
			        // set type, either numeric or text 
			        type: 'text' 
			    });				    																		
				serviceTable.tablesorter({
					headers: {
						4: {
							sorter: 'created'
						}
					}					})
				.tablesorterPager({
					container: $("#dailyPaging"),
					size: 10,										
					positionFixed: false
				});
			} else {
				$("#dailyPaging").hide();
			}
			$('div#dailyResult table tr:last-child').addClass('last');
		},
		"json"
	);
}

function demogSearch(){
	if ($.trim($('#code').val()) != "" || $.trim($('#name').val()) != "" ||
	    $.trim($('#date').val()) != "" || $.trim($('#email').val()) != "" ||
	    $('#male').is(':checked') || $('#female').is(':checked') ||
	    $.trim($('#phone').val()) != "" || $.trim($('#career').val()) != "" ||
	    $.trim($('#street').val()) != "" || $.trim($('#ward').val()) != "" ||		    
	    $.trim($('#district').val()) != "" || $.trim($('#province').val()) != "") {
	    $('#demographicsSearchBtnDiv').css('display', 'none');
		$('#loading').css('display', 'block');
		$.get (
			'searchAjax?set_ajax_view',
			{ 
				search_type: 'demographic_search',	
				code: $('#code').val(), name: $('#name').val(),
				female: $('#female').is(':checked'), 
				male: $('#male').is(':checked'),
				phone: $('#phone').val(), career: $('#career').val(),
				email: $('#email').val(), date: $('#date').val(),
				street: $('#street').val(), ward: $('#ward').val(),
				district: $('#district').val(), province: $('#province').val()
			},
			function(data) {
				$('div#demographicsResult').html(data.header + data.body);
				$('#demographicsSearchBtnDiv').css('display', 'inline');
				$('#loading').css('display', 'none');
				// Update default number of rows per page
				$('#demoPaging select').val('10');
				
				serviceTable = $('div#demographicsResult table');
				if ($('tbody tr', serviceTable).length > 1) {
					$("#demoPaging").show();
					serviceTable.trigger("update");																			
					serviceTable.tablesorter().tablesorterPager({
						container: $("#demoPaging"),
						size: 10,										
						positionFixed: false
					});
				} else {
					$("#demoPaging").hide();
				}
				$('div#demographicsResult table tr:last-child').addClass('last');
			},
			"json"
		);
	}
};

function smartSearch(){
	var str = $('#searchstring').val();		
	while (str.indexOf('  ') != '-1'){
		$('#searchstring').val(str.split('  ').join(' '));
		str = $('#searchstring').val();
	}
	if ($.trim($('#searchstring').val()) != "") {
		$('#smartSearchBtnDiv').css('display', 'none');
		$('#smartLoading').css('display', 'block');
		$.get (
			'searchAjax?set_ajax_view',
			{ searchstring: $('#searchstring').val(), search_type: 'smart_search' },
			function(data) {
				$('div#smartResult').html(data.header + data.body);
				$('#smartSearchBtnDiv').css('display', 'inline');
				$('#smartLoading').css('display', 'none');
				
				// Update default number of rows per page
				$('#smartPaging select').val('10');	
				$('div#smartResult table tr:last-child').addClass('last');
			},
			"json"
		);			
	}
}

function reverse(name) {
	$('#'+name).attr('checked', false);
}

function isReadyToSearch(id) {
	switch (id){
	case 'smartSearchBtn':
		if ($.trim($('#searchstring').val()) == "") {
			$('#searchstring').val('');
			$('#statusMessageSmart').html(_("Input patient's code or name"));
		} else {
			$('#statusMessageSmart').html('');
		}	
		break;
	case 'dailySearchBtn':		
		if ($.trim($('#daily_code').val()) != "" || $.trim($('#daily_name').val()) != ""){
			$('#statusMessageDaily').html('');
		} else {
			$('#statusMessageDaily').html(_('Input any characters or click on button to view all'));
		}
		break;
	case 'demographicsSearchBtn':
		if ($('#code').val() != "" || $('#name').val() != "" ||
			$('#date').val() != "" || $('#email').val() != "" ||
		    $('#male').is(':checked') || $('#female').is(':checked') ||
		    $('#phone').val() != "" || $('#career').val() != "" ||
		    $('#street').val() != "" || $('#ward').val() != "" ||		    
		    $('#district').val() != "" || $('#province').val() != ""){
		    $('#statusMessageDemo').html('')
		 } else {
			$('#statusMessageDemo').html(_('Input any characters'));
		 }  	
		break;
	}
}	

var curDivId = 'demographicsSearch'
function showOneDiv(divId) {
	if (divId != curDivId) {
		$("#" + curDivId).hide("slow");
		$("#" + divId).show("slow");
		curDivId = divId;
	}
}

function viewLatestPrescription(patientId) {
	$('span#jqmName').text(_('Prescription'));
	$('#latestRefContent').hide();	
	$('#printViewLoading').show();
	var buttons = {};
	buttons[_('Close')] = function() {		
		$(this).dialog('close');		
	};
	buttons[_('Print')] = function() {
		var recordId = $("#printPrescription").val();
		if (recordId != null && recordId != '' ) {
			printPrescription(recordId, 'no', 'vi')
			$(this).dialog('close');
		}		
	};
	generalDialog('latestRefraction',null,null,null,{
		autoOpen: true,			
		bgiframe: true,
		resizable: true,		
		width: 700,
		height: 400,
		modal: true,
		buttons:buttons
	},_('Print'));
	$.ajax ({
		type: 	"GET",
		url:	"../Prescription/getLatestPrescription?set_ajax_view",
		data:	"patientId=" + patientId,
		dataType: "html",
		success: function(data) {
			$('#latestRefContent').html(data);
			$('#printViewLoading').hide();
			$('#latestRefContent').show();			
		}
	});
}